package com.cdtu.controller;

import com.cdtu.entity.Admin;
import com.cdtu.service.AdminService;
import com.cdtu.service.OperationRecordService;
import com.cdtu.vo.OperationRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : Sakura
 * @Date : 2023/1/5 14:52
 */

@RestController
@RequestMapping("/record")
//解决跨域问题
@CrossOrigin
public class OperationRecordController {

    @Autowired
    private OperationRecordService recordService;

    @Autowired
    private AdminService adminService;

    @GetMapping("/findByPage")
    public Map<String, Object> findByPage(Integer pageNow,Integer pageSize){

        Map<String, Object> result=new HashMap<>();

        pageNow=pageNow==null?1:pageNow;
        pageSize=pageSize==null?20:pageSize;

        List<OperationRecordVO> recordVOList = recordService.findByPage(pageNow, pageSize);
        for (OperationRecordVO recordVO : recordVOList) {
            String id = recordVO.getCheckUser();
            Admin admin = adminService.getById(id);
                recordVO.setCheckUser(admin.getName());

        }

        long total = recordService.findTotal();

        result.put("records",recordVOList);
        result.put("total",total);
        return result;
    }

    @GetMapping("/findByDate")
    public Map<String, Object> findByDate(Integer pageNow,Integer pageSize,  String value){

        Map<String, Object> result=new HashMap<>();

        pageNow=pageNow==null?1:pageNow;
        pageSize=pageSize==null?20:pageSize;

        List<OperationRecordVO> recordVOList = recordService.findByDate(pageNow, pageSize, value);

        for (OperationRecordVO recordVO : recordVOList) {
            String id = recordVO.getCheckUser();
            Admin admin = adminService.getById(id);
            recordVO.setCheckUser(admin.getName());

        }

        long total = recordService.findByDateTotal(value);

        result.put("records",recordVOList);
        result.put("total",total);
        return result;
    }


}
